/**
 * @author zyj
 * @date 2022年06月10日 10:11
 */
public class Test3 {
    public static void main(String[] args) {
        synchronizedDemo sy = new synchronizedDemo();
        MyRunnable myRunnable = new MyRunnable();
        new Thread(myRunnable,"线程A"){
            @Override
            public void run() {
                sy.add();
            }
        }.start();
        new Thread(myRunnable,"线程B"){
            @Override
            public void run() {
                sy.add();
            }
        }.start();
    }
}

class synchronizedDemo {
    private volatile int num = 0;
    public void add(){
        synchronized (this){
            System.out.println(Thread.currentThread().getName() + ":" + num);
            num++;
        }
    }
}
class MyRunnable implements Runnable{
    @Override
    public void run() {
    }
}